import 'package:permission_handler/permission_handler.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'state.dart';

class TakePicSettingLogic extends BaseController {
  final TakePicSettingState state = TakePicSettingState();
  static const methodSendCameraPermission = "sendCameraPermission";

  @override
  Future<void> onReady() async {
    var hasPermission = await Permission.camera.isGranted;
    if (!hasPermission) {
      var value = await Permission.camera.request();
      if(!value.isGranted){
        showPermissionDialog(LocaleKeys.contact_permission_tips.ts);
      }
    }
    super.onReady();
  }


  void sendCameraPermission(bool value) {
    showLoading();
    state.openShakeToTakePic.value = value;
    bRemoteReqAsync(methodSendCameraPermission, {UTEPedConst.keyEnable: value})
        .then((success) => _onSendCameraPermission(success, value));
  }

  void _onSendCameraPermission(success, value) {
    cancelLoading();
    if (success) {
      save(methodSendCameraPermission, value);
    }
    else {
      state.openShakeToTakePic.value = !value;
    }
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  void initState() {
    state.openShakeToTakePic.value = read(methodSendCameraPermission, defaultValue: false);
  }

  @override
  void onRemoteEvent(event, method) {
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

}
